﻿using Moneyman.Enums;
using Moneyman.Infrastructure;
using System.Data.Linq.Mapping;
using System.Windows.Resources;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Markup;
using System;
using System.IO;
using System.Linq;

namespace Moneyman.DataModel
{
    [Table(Name = "Categories")]
    public partial class CategoryInfo : NotificationObject
    {
        public CategoryInfo() { }

        #region Fields
        private string guid = System.Guid.NewGuid().ToString();
        private string name;
        private string description;
        private bool _default = false;
        private TransactionKindValue kind = TransactionKindValue.Expense;
        private string iconResourceKey = "PATH_CATE_DEFAULT";
        #endregion

        #region Properties
        /// <summary>
        /// Get,Set
        /// </summary>
        [Column(IsPrimaryKey = true)]
        public string Guid
        {
            get { return guid; }
            set
            {
                guid = value;
                RaisePropertyChanged(() => Guid);
            }
        }

        /// <summary>
        /// Get,Set
        /// </summary>
        [Column]
        public string Name
        {
            get { return name; }
            set
            {
                name = value;
                RaisePropertyChanged(() => Name);
            }
        }

        /// <summary>
        /// Get,Set
        /// </summary>
        [Column]
        public string Description
        {
            get { return description; }
            set
            {
                description = value;
                RaisePropertyChanged(() => Description);
            }
        }

        /// <summary>
        /// Get,Set
        /// </summary>
        [Column]
        public bool Default
        {
            get { return _default; }
            set
            {
                _default = value;
                RaisePropertyChanged(() => Default);
            }
        }

        /// <summary>
        /// Get,Set
        /// </summary>
        [Column]
        public TransactionKindValue Kind
        {
            get { return kind; }
            set
            {
                kind = value;
                RaisePropertyChanged(() => Kind);
            }
        }

        /// <summary>
        /// Get,Set
        /// </summary>
        [Column]
        public string IconResourceKey
        {
            get { return iconResourceKey; }
            set
            {
                iconResourceKey = value;
                RaisePropertyChanged(() => IconResourceKey);
            }
        }

        public double Sum { get; set; }

        /// <summary>
        /// Get
        /// </summary>
        public double Percent { get; set; }

        /// <summary>
        /// Get
        /// </summary>
        public double TransactionCount { get; set; }

        /// <summary>
        /// Get
        /// </summary>
        public IEnumerable<string> CategoryIcons
        {
            get
            {
                StreamResourceInfo rd = Application.GetResourceStream(new Uri("/Moneyman.Wallet;Component/Data/PathData.xaml", UriKind.RelativeOrAbsolute));
                ResourceDictionary d = (ResourceDictionary)XamlReader.Load(new StreamReader(rd.Stream).ReadToEnd());

                var coll = d.Where(r => r.Key.ToString().StartsWith("PATH_CATE"));

                foreach (KeyValuePair<object, object> kvp in coll)
                {
                    yield return kvp.Key.ToString();
                }
            }
        }
        #endregion
    }
}
